<template>
  <div class="goods">
    <van-nav-bar title="商品展示" left-text="返回" left-arrow @click-left="back" />

    <div class="list">
      <van-swipe :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="(item,index) in product.imgList " :key="item.id">
          <img :src="'http://www.mianshiti.top' + item.path ">
        </van-swipe-item>
      </van-swipe>
      <van-cell-group>
        <van-cell>
          <div class="goods-title">{{ product.name }}</div>
          <div class="goods-price">{{ product.price }}</div>
        </van-cell>
        <van-cell class="goods-express">
          <van-col span="10">免运费</van-col>
          <van-col span="14">剩余：{{ product.stock }}件</van-col>
          <van-col span="10">销量：{{ product.sales }}</van-col>
          <van-col span="14">收藏数：{{ product.likes }}件</van-col>
        </van-cell>
      </van-cell-group>

      <van-tabs>
        <van-tab title="详情">
          <div v-for="(item,index) in product.detailImgList" :key="item.id">
            <img :src="'http://www.mianshiti.top' + item.path">
          </div>
        </van-tab>
        <van-tab title="参数">
          <van-cell class="goods-express" v-for="(item,index) in product.properties" :key="'product-'+item.id">
            <van-col span="10">{{item.name}}</van-col>
            <van-col span="14">{{item.detail }}</van-col>
          </van-cell>
        </van-tab>
      </van-tabs>
    </div>
    <van-goods-action>
      <van-goods-action-icon :class="{'red':isLike}" :icon="isLike ? 'like' : 'like-o'" @click="like" text="收藏" />
      <!-- $router.push('/gwc')页面跳转 -->
      <van-goods-action-icon icon="cart-o" text="购物车" @click = " $router.push('/gwc')"/>
      <van-goods-action-button type="warning" text="加入购物车" @click="addcart" />
      <van-goods-action-button type="danger" text="立即购买" />
    </van-goods-action>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        product: {},
      }
    },
    computed: {
      id() {
        return this.$route.params.id
      },
      isLike() {
        if (!Array.isArray(this.$store.user.likeList)) {
          return false
        }
        for (let i = 0; i < this.$store.user.likeList.length; i++) {
          if (this.$store.user.likeList[i].product_id == this.id) {
            return true
          }
        }
        return false
      }
    },
    methods: {
      addcart() {
        this.$axios.post('/cart/update', {
          product_id: this.id
        }).then(res => {
          if (res.errcode == 0) {
            this.$store.cart = res.data
          } else if (res.errcode == 90101) {
            this.$store.openSignin(true)
          }
        })
      },
      like() {
        if (this.isLike) {
          this.$axios.get(`/product_like/delete?product_id=${this.id}`, {
          }).then(res => {
            if (res.errcode == 0) {
              for (let i = 0; i < this.$store.user.likeList.length; i++) {
                if (this.$store.user.likeList[i].product_id = this.id) {
                  this.$store.user.likeList.splice(i, 1)
                  break
                }
              }
            }
          })
        } else {
          this.$axios.post('/product_like/create', {
            product_id: this.$route.params.id
          }).then(res => {
            if (res.errcode == 0) {
              this.product.product_id = this.id
              this.$store.user.likeList.push(this.product)
            } else if (res.errcode == 90101) {
              this.$store.showSignin = true
            }
          })
        }

      },
      back() {
        this.$router.back()
      }
    },
    mounted() {
      console.log(this.$route.params.id);
      this.$axios.get(`product?id=${this.$route.params.id}`).then(res => {
        if (res.errcode == 0) {
          this.product = res.data
        }
      })
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
  .van-swipe-item img,
  .van-tab__pane img {
    width: 100vw;
  }

  .list {
    height: calc(100vh - 96px);
    overflow: scroll;
    -webkit-overflow-scrolling: touch;
  }
</style>